#include <iostream>
#include <algorithm>

using namespace std;
const int N = 2e5 + 10;

struct node
{
	int x, y;
}a[N];
bool cmp(node& a, node& b)
{
	return a.y < b.y;
}
int f[N], g[N];
int n;
int main()
{
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i].x >> a[i].y;
	}
	sort(a + 1, a + 1 + n, cmp);
	for (int i = 1; i <= n; i++)
	{
		int l = 0, r = i - 1;
		while (l < r)
		{
			int mid = (l + r + 1) / 2;
			if (a[mid].y < a[i].x) l = mid;
			else r = mid - 1;
		}
		f[i] = g[l] + a[i].y - a[i].x + 1;
		g[i] = max(f[i], g[i - 1]);
	}
	cout << g[n] << endl;
	return 0;
}